﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using Aquarinum.WebSocekt.Client;
using Aquarinum.WebSocekt.Client.Core.Interfaces;
using Aquarinum.WebSocekt.Core.Dtos;
using Aquarinum.WebSocekt.Core.Utils;
using Newtonsoft.Json;

namespace WebsocketClientDemo.NET35
{
    public partial class MainWindow : Window
    {
        private IWebsocketClient _websocketClient;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (_websocketClient == null)
            {
                MessageBox.Show("先连接");
                return;
            }

            var dto = JsonConvert.DeserializeObject<WebsocketMessageDto>(TxtSend.Text);
            _websocketClient.SendMessage(dto);
        }

        private void Conn_OnClick(object sender, RoutedEventArgs e)
        {
            _websocketClient = WebsocketClientFactory.GetDefaultClient();
            _websocketClient.InitClient(Txtuser.Text, Txtresource.Text, Txthost.Text);

            _websocketClient.OnClientOpened += (o, args) => { MessageBox.Show("客户端打开成功"); };
            _websocketClient.OnClientUnAuth += (o, args) => { MessageBox.Show("授权失败"); };
            _websocketClient.OnClientClosed += (o, args) => { MessageBox.Show("连接关闭"); };
            _websocketClient.OnClientConnected += (o, args) => { MessageBox.Show("连接成功"); };
            _websocketClient.OnMessageReceipt += (o, args) => { MessageBox.Show("收到回执消息"); };
            _websocketClient.OnRecivedMessage += (o, args) =>
            {
                UiThread.Invoke(() => { TxtRe.Text = JsonConvert.SerializeObject(args.Message); });
            };
            _websocketClient.ConnectServer();
        }


        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
        }


        private void p2pButton_OnChecked(object sender, RoutedEventArgs e)
        {
            var dto = new WebsocketMessageDto();
            dto.Id = Guid.NewGuid().ToString("N");
            dto.From = Txtuser.Text + ";" + Txtresource.Text;
            dto.To = Txtuser.Text + ";" + Txtresource.Text;
            dto.Domain = Txthost.Text;
            dto.Timestamp = DateTimeHelper.GetUnixTimestamp();
            dto.Type = "P2P";
            TxtSend.Text = JsonConvert.SerializeObject(dto);
        }

        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            var dto = new WebsocketMessageDto
            {
                Id = Guid.NewGuid().ToString("N"),
                Domain = Txthost.Text,
                From = Txtuser.Text + ";" + Txtresource.Text,
                Timestamp = DateTimeHelper.GetUnixTimestamp(),
                Type = "Publish"
            };
            TxtSend.Text = JsonConvert.SerializeObject(dto);
        }
    }

    public class UiThread
    {
        public static bool IsUiThread()
        {
            return
                Application.Current.Dispatcher.Thread.ManagedThreadId ==
                Thread.CurrentThread.ManagedThreadId;
        }

        public static void Invoke(Action action)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Send, action);
        }

        public static void Invoke(Action action, DispatcherPriority priority)
        {
            Application.Current.Dispatcher.Invoke(priority, action);
        }

        public static void BeginInvoke(Action action)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Render, action);
        }

        public static void BeginInvoke(Action action, DispatcherPriority priority)
        {
            Application.Current.Dispatcher.BeginInvoke(priority, action);
        }
    }


    public class TestMessage
    {
        public string Message { get; set; }
    }
}